layui.use(['element','table','laytpl','form'],function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer,
        form=layui.form;
    var mbcols= [[ //表头
        {type:'checkbox'}
        ,{title: '序号',type:'numbers'}
        ,{ title: '编号',templet:function(d){
                return d.buildingName+'-'+d.roomName+'-'+d.code;
            }}
        ,{field: 'typeName', title: '床位类型'}
        ,{field: 'status', title: '床位状态',templet:function (d) {
                switch (d.status){
                    case 1:return '空闲';
                    case 2:return '已住';
                    case 3:return '在建';
                    case 4:return '停用';
                    default:return'';
                }
            }}
        ,{field: 'price', title: '价格',templet:function(d){
                return d.price + " 元"
            }}
        ,{title:'操作',toolbar:"#toolbar",width:"18%"}
    ]];
    var gbcols=[[ //表头
        {type:'checkbox'}
        ,{title: '序号',type:'numbers'}
        ,{ title: '编号',templet:function(d){
                return d.buildingName+'-'+d.roomName+'-'+d.code;
            }}
        ,{field: 'type', title: '床位类型',templet:function (d) {
                switch (d.type){
                    case 1:return '普通床位';
                    case 2:return '护理床位';
                    default:return'';
                }
            }}
        ,{field: 'status', title: '床位状态',templet:function (d) {
                switch (d.status){
                    case 1:return '空闲';
                    case 2:return '已住';
                    case 3:return '在建';
                    case 4:return '停用';
                    default:return'';
                }
            }}
        ,{title:'操作',toolbar:"#toolbar",width:"18%"}
    ]];
    var cols="";
    $(function () {
        if ($("#usertype").val()==2){
            cols=mbcols;
        }else  if ($("#usertype").val()==1){
            cols=gbcols;
        }
    })
  var tableIns= table.render({
        elem: '#demo'
        ,id:"mytable"
        ,url: '/bed/list' //数据接口
        ,page: true //开启分页
        ,limit:15
        ,limits:[15,30,60,100]
        ,response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,msgName: 'msg' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'rows' //数据列表的字段名称，默认：data
        }
        ,cols: cols
    });

    //工具条
    table.on("tool(test)",function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if("edit" == layEvent){
            location.href = "/bed/edit?id=" + data.id;
        }else if("delete" == layEvent){
            var  params ="id=" +  data.id;
            $.ajax({
                type: 'post',
                url: '/bed/selecBedtList',
                dataType: 'json',
                data: params,
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("该床住有老人，不能删除。");
                    } else {
                        location.href = "/bed/delete?id=" + data.id;
                    }
                }
            });
        }
    });

    var buildingId;
    form.on('select(buildingNo)', function(data){
        buildingId = data.value;
        if(data.value){
            $.ajax({
                url:"/room/floors",
                data:{buildingId:buildingId},
                dataType:'json',
                success:function(rs){
                    $("#floorNo").empty().append('<option value="">请选择楼层</option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#floorNo").append('<option value="'+ r +'">'+ r +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });

    form.on('select(floorNo)', function(data){
        var floor = data.value;
        if(data.value){
            $.ajax({
                url:"/bed/rooms",
                data:{buildingId:buildingId,floor:floor},
                dataType:'json',
                success:function(rs){
                    $("#roomId").empty().append('<option value=""></option>');
                    if(rs.code == 200){
                        $.each(rs.data,function(i,r){
                            $("#roomId").append('<option value="'+ r.id +'">'+ r.code +'</option>');
                        });
                    }
                    form.render('select'); //刷新select选择框渲染
                }
            });
        }
    });
    /**
     * 搜索
     */
    $("#search").on('click',function(){
        var params = $("#search-form").serializeObject();
        tableIns.reload({
            where: params //设定异步数据接口的额外参数
        });
    });

});